package com.rc.saas.tenant.service.hlht;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.mapper.elec.ElecGunMapper;
import com.rc.saas.tenant.mapper.hlht.HlhtOperatorMapper;
import com.rc.saas.tenant.mapper.hlht.HlhtTenantOpenStationMapper;
import com.rc.saas.tenant.mapper.hlht.HlhtTenantOpenforMapper;
import com.rc.saas.tenant.model.elec.ElecGun;
import com.rc.saas.tenant.model.elec.ElecGunExample;
import com.rc.saas.tenant.model.hlht.*;
import com.rc.saas.tenant.search.hlht.HlhtTenantOpenforSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Create by Yang on 2021/6/22
 */
@Service
public class HlhtTenantOpenforServiceImpl implements HlhtTenantOpenforService {

    @Autowired
    private HlhtTenantOpenforMapper hlhtTenantOpenforMapper;

    @Autowired
    private HlhtOperatorMapper hlhtOperatorMapper;

    @Autowired
    private ElecGunMapper elecGunMapper;

    @Autowired
    private HlhtTenantOpenStationMapper hlhtTenantOpenStationMapper;

    @Override
    public HlhtTenantOpenforMapper getMapper() {
        return hlhtTenantOpenforMapper;
    }

    @Override
    public PageInfo<HlhtTenantOpenfor> listPageHlhtTenantOpenfor(HlhtTenantOpenforSearch hlhtTenantOpenforSearch) {
        //是否需要分页,默认不分页
        if (hlhtTenantOpenforSearch.getIsPaeg() != null) {
            PageHelper.startPage(hlhtTenantOpenforSearch.getOffset(), hlhtTenantOpenforSearch.getLimit());
        }

        //查询运营商内部反向互联开放的第三方互联互通列表
        HlhtTenantOpenforExample example = new HlhtTenantOpenforExample();
        example.createCriteria().andTenantCodeEqualTo(hlhtTenantOpenforSearch.getTenantCode())
                .andHlhtTenantCodeEqualTo(hlhtTenantOpenforSearch.getHlhtTeanantCode());
        example.setOrderByClause(" tenant_openfor_id DESC ");

        List<HlhtTenantOpenfor> hlhtTenantOpenfors = hlhtTenantOpenforMapper.selectByExample(example);
        return new PageInfo<>(hlhtTenantOpenfors);
    }

    @Override
    public HlhtTenantOpenfor selectTenantOpenforByGunNo(String tenantCode, String gunNo) {

        ElecGunExample elecGunExample = new ElecGunExample();
        elecGunExample.createCriteria().andGunNoEqualTo(gunNo);
        List<ElecGun> elecGuns = elecGunMapper.selectByExample(elecGunExample);

        //通过枪编号获取站点ID
        ElecGun elecGun = elecGuns.get(0);

        HlhtTenantOpenforExample example = new HlhtTenantOpenforExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode)
                .andElecStationIdEqualTo(elecGun.getElecStationId());
        example.setOrderByClause(" tenant_openfor_id DESC LIMIT 1");

        List<HlhtTenantOpenfor> hlhtTenantOpenfors = hlhtTenantOpenforMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(hlhtTenantOpenfors)) {
            return null;
        }
        return hlhtTenantOpenfors.get(0);
    }

    /**
     * 查询运营商是否通过瑞晨开放给第三方
     * @param tenantCode
     * @param hlhtOperatorId
     * @return
     */
    @Override
    public HlhtTenantOpenfor listByHlhtOperatorId(String tenantCode, String hlhtOperatorId) {
        HlhtTenantOpenforExample example = new HlhtTenantOpenforExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode)
                .andHlhtOperatorIdEqualTo(hlhtOperatorId);
        example.setOrderByClause(" tenant_openfor_id DESC LIMIT 1");

        List<HlhtTenantOpenfor> hlhtTenantOpenfors = hlhtTenantOpenforMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(hlhtTenantOpenfors)) {
            return null;
        }
        return hlhtTenantOpenfors.get(0);
    }

    @Override
    public int deleteByElecStationIds(List<Long> elecStationIds, String tenantCode, String hlhtTenantCode) {
        //查询运营商内部反向互联开放的第三方互联互通列表
        return hlhtTenantOpenforMapper.deleteByElecStationIds(elecStationIds, tenantCode, hlhtTenantCode);
    }

    @Override
    public void updateOpenforStatusByStationIds(List<Long> elecStationIds, String hlhtOperatorId, String openforStatus) {
        hlhtTenantOpenforMapper.updateOpenforStatusByStationIds( elecStationIds, hlhtOperatorId,openforStatus);
    }

    @Override
    public int deleteByHlhtElecStationIds(List<Long> hlhtElecStationIds, String tenantCode, String hlhtTenantCode) {
        //互联互通ID转站点ID
        List<Long> elecStationIds = new ArrayList<>();
        for (Long hlhtStationId : hlhtElecStationIds) {
            HlhtTenantOpenStation hlhtTenantOpenStation = hlhtTenantOpenStationMapper.selectByPrimaryKey(hlhtStationId);
            if (hlhtTenantOpenStation != null) {
                elecStationIds.add(hlhtTenantOpenStation.getElecStationId());
            }
        }

        //添加之前先删除之前记录
        hlhtTenantOpenforMapper.deleteByElecStationIds(elecStationIds, tenantCode, hlhtTenantCode);
        return 0;
    }

    @Override
    public int insertByElecStationIds(List<Long> hlhtElecStationIds, String tenantCode, String hlhtTenantCode, List<String> hlhtOperatorIds) {

        //互联互通ID转站点ID
        List<Long> elecStationIds = new ArrayList<>();
        for (Long hlhtStationId : hlhtElecStationIds) {
            HlhtTenantOpenStation hlhtTenantOpenStation = hlhtTenantOpenStationMapper.selectByPrimaryKey(hlhtStationId);
            if (hlhtTenantOpenStation != null) {
                elecStationIds.add(hlhtTenantOpenStation.getElecStationId());
            }
        }

        //添加之前先删除之前记录
        hlhtTenantOpenforMapper.deleteByElecStationIds(elecStationIds, tenantCode, hlhtTenantCode);

        //遍历所有站点与运营商进行插入，多对多关系
        for (Long elecStationId : elecStationIds) {

            for (String hlhtOperatorId : hlhtOperatorIds) {

                //查询第三方互联方
                HlhtOperatorExample example = new HlhtOperatorExample();
                example.createCriteria().andHlhtOperatorIdEqualTo(hlhtOperatorId).andStatusEqualTo("0")
                        .andOperatorTypeEqualTo("0");
                List<HlhtOperator> hlhtOperators = hlhtOperatorMapper.selectByExample(example);
                if (CollectionUtils.isEmpty(hlhtOperators)) {
                    continue;
                }
                HlhtOperator hlhtOperator = hlhtOperators.get(0);

                //添加设备方对反向saas运营商开放的第三方运营商站点
                HlhtTenantOpenfor hlhtTenantOpenfor = new HlhtTenantOpenfor();
                hlhtTenantOpenfor.setHlhtTenantCode(tenantCode);
                hlhtTenantOpenfor.setHlhtOperatorId(hlhtOperatorId);
                hlhtTenantOpenfor.setHlhtTenantCode(hlhtTenantCode);
                hlhtTenantOpenfor.setHlhtOperatorName(hlhtOperator.getHlhtOperatorName());
                hlhtTenantOpenfor.setHlhtOperatorShortname(hlhtOperator.getHlhtOperatorShortname());
                hlhtTenantOpenfor.setHlhtOpenType("0");//开放类型,0 SAAS运营商 1商业运营平台 2政府平台
                hlhtTenantOpenfor.setHlhtType("0");//开放类型 0.反向对接 1.正向对接

                hlhtTenantOpenfor.setElecStationId(elecStationId);//关联站点ID
                hlhtTenantOpenfor.setCreatedTime(new Date());
                hlhtTenantOpenfor.setOpenforStatus("1");//是否开放第三方 0是,1否。默认0 添加记录但不开放

                hlhtTenantOpenforMapper.insertSelective(hlhtTenantOpenfor);
            }
        }

        return 1;
    }

    @Override
    public void updateOpenforStatus(List<Long> hlhtElecStationIds, String hlhtOperatorId, String tenantCode) {

    }

}
